package com.open.capacity.workflow.service.impl;


import com.open.capacity.commons.Result;
import com.open.capacity.finance.util.uuid.UUIDUtils;
import com.open.capacity.workflow.dao.WFSchemeAuthorizeDao;
import com.open.capacity.workflow.model.WFSchemeAuthorize;
import com.open.capacity.workflow.service.WFSchemeAuthorizeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class WFSchemeAuthorizeServiceImpl implements WFSchemeAuthorizeService {

    @Autowired
    private WFSchemeAuthorizeDao schemeAuthorizeDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(WFSchemeAuthorize confluence) {

        schemeAuthorizeDao.save(confluence);
        log.info("保存流程授权：{}", confluence);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(WFSchemeAuthorize confluence) {

        schemeAuthorizeDao.updateByOps(confluence);
        log.info("修改流程授权：{}", confluence);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        WFSchemeAuthorize confluence = schemeAuthorizeDao.findById(id);

        schemeAuthorizeDao.delete(id);

        log.info("删除流程授权：{}", confluence);
    }


    @Override
    public WFSchemeAuthorize findById(String id) {
        return schemeAuthorizeDao.findById(id);
    }

    @Override
    public List<WFSchemeAuthorize> findBySchemeinfoId(String schemeinfoId) {
        return schemeAuthorizeDao.findBySchemeinfoId(schemeinfoId);
    }


    @Override
    public Result saveOrUpdate(WFSchemeAuthorize confluence, String keyValue) {
        int i = 0;

        if (StringUtils.isNotEmpty(keyValue)) {
            WFSchemeAuthorize model = schemeAuthorizeDao.findById(keyValue);
            if (model != null) {
                confluence.setId(keyValue);
                i = schemeAuthorizeDao.updateByOps(confluence);
            }
        } else {
            confluence.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            i = schemeAuthorizeDao.save(confluence);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }


}
